<template>
  <div style="position: relative">
    <div ref="echartDom" v-bind="$attrs"></div>
    <div class="check-time-year-or-month" :style="{...magnifyFontSizeObj}">
      <el-button :class="lineData?.xAxisTimeType === 'year' ? 'active-btn' : ''" @click.stop="changeXAxisTimeType('year')">按年</el-button>
      <el-button :class="lineData?.xAxisTimeType === 'month' ? 'active-btn' : ''" @click.stop="changeXAxisTimeType('month')">按月</el-button>
    </div>
  </div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose, defineEmits} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  lineData: {
    type: Object,
    default: () => ({})
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const emit = defineEmits(['changeXAxisTimeType', 'dialogEmit'])

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const magnifyFontSizeObj = ref({
  '--custom-font-size-10': pxToRem(props?.dialogData?.fontSizeTimes * 10) + 'px',
  '--custom-font-size-20': pxToRem(props?.dialogData?.fontSizeTimes * 20) + 'px',
  '--custom-font-size-20-30': pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 20 : 60) + 'px',
})

const tempDom = (params) => {
  let tempDom = ''
  const tempTbClass = params?.data?.percentageYoy > 0 ? 'triangle-reverse' : ''
  const tempHbClass = params?.data?.percentageMom > 0 ? 'triangle-reverse' : ''
  const tempTbColor = params?.data?.percentageYoy > 0 ? '#01FEFF' : '#E13535'
  const tempHbColor = params?.data?.percentageMom > 0 ? '#01FEFF' : '#E13535'

  switch(props?.lineData?.xAxisTimeType) {
    case 'month':
      tempDom = '<br/>' + `<div style="display: flex; align-items: center"><div class="triangle ${tempTbClass}"></div>&nbsp;<span style="color: ${tempTbColor}">${params.data?.percentageYoy}%</span>` + `&nbsp;同比</div>`
          + `<div style="display: flex; align-items: center"><div class="triangle ${tempHbClass}"></div>&nbsp;<span style="color: ${tempHbColor}">${params.data?.percentageMom}%</span>` + `&nbsp;环比</div>`
      break
    case 'year':
      tempDom = '<br/>' + `<div style="display: flex; align-items: center"><div class="triangle ${tempTbClass}"></div>&nbsp;<span style="color: ${tempTbColor}">${params.data?.percentageYoy}%</span>` + `&nbsp;同比</div>`
      break
    default:
      tempDom = ''
      break
  }
  return tempDom
}

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let option = {
      legend: {
        x: 'center',
        padding: [pxToRem(15), 0, pxToRem(30 * props?.dialogData?.fontSizeTimes)],
        itemWidth: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        itemHeight: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        textStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes)
        },
        icon: "rect",
      },
      grid: {
        left: 0,
        right: '10%',
        bottom: 0,
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 35 : 20}%`,
        containLabel: true
      },
      dataZoom: { // 数据过多内部拖动逻辑
        type: "slider",
        startValue: 0,
        endValue: 5,
        height: pxToRem(20),
        bottom: pxToRem(10)
      },
      tooltip: {
        formatter: function (params) {
          return (
              `${params.seriesName}面积：` + `${params.value}万㎡` + tempDom(params)
          )
        }
      },
      xAxis: {
        min: 0,
        type: 'category',
        boundaryGap: false,
        data: props?.lineData?.xAxisData,
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter',
          showMinLabel: false
        },
        axisLine: {
          show: false // 不显示坐标轴线
        }
      },
      yAxis: {
        min: 0,
        max: (value) => value.max || 50000,
        textStyle: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes)
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter'
        },
        name: "面积：万㎡",
        nameTextStyle: {
          color: "white",
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          fontWeight: 'lighter',
          padding: [0, 0, 0, pxToRem(30 * props?.dialogData?.fontSizeTimes)]
        }
      },
      series: props?.lineData?.seriesData,
    }
    option && myChart.value.setOption(option, true);

    myChart.value.on("legendselectchanged", (params) => {
      event.stopPropagation();
    });
  }
}

const changeFontSize = () => { // 动态改变字体大小方法
  magnifyFontSizeObj.value = {
    '--custom-font-size-10': pxToRem(props?.dialogData?.fontSizeTimes * 10) + 'px',
    '--custom-font-size-20': pxToRem(props?.dialogData?.fontSizeTimes * 20) + 'px',
    '--custom-font-size-20-30': pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 20 : 60) + 'px',
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

const changeXAxisTimeType = (val) => {
  emit('changeXAxisTimeType', val)
  emit('dialogEmit', {type: props?.lineData?.moduleName, value: val})
}


defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h250 {
  height: 250px;
}

.dialog-h250 {
  width: 1120px;
  height: 500px;
}

.h201 {
  height: 201px;
}

.h216 {
  height: 216px;
}

.check-time-year-or-month {
  position: absolute;
  right: 0;
  bottom: 30%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .el-button {
    width: var(--custom-font-size-20-30);
    height: var(--custom-font-size-20);
    margin: 0;
    font-size: var(--custom-font-size-10);
    box-shadow: inset 0 0 0.04167rem 0 #3D7FFF;
    border-radius: 0.04167rem;
    cursor: pointer;
    color: white;
    background-color: transparent;
    border: 0;
  }

  .el-button:last-child {
    margin-top: 5px;
  }

  .el-button.active-btn {
    background: linear-gradient(60deg, #0E5FFF 0%, #00FFFF 100%);
  }
}

:deep(.triangle) {
  width: 0;
  height: 0;
  border: 5px solid transparent;
  border-top: 5px solid #E13535;
  border-bottom: 0;
  display: inline-flex;
  align-items: center;
  margin-right: 5px;
}

:deep(.triangle-reverse) {
  transform: rotate(180deg);
  border-top: 5px solid #01FEFF;
}
</style>